//
//  FloatPickerCellData.m
//  18Birdies
//
//  Created by 张永 on 2017/8/11.
//  Copyright © 2017年 Lc. All rights reserved.
//

#import "FloatPickerCellData.h"
#import "LcFoundation.h"
#import "NumberPickerCell.h"

@implementation FloatPickerCellData

+ (FloatPickerCellData*)cellDataWithValues:(NSArray*)values
                                      value:(float)value {
  LC_CHECK_NOT_EMPTY(values);
  FloatPickerCellData* cellData = [[self alloc] init];
  cellData.values = values;
  cellData.value = value;
  cellData.clickable = NO;
  cellData.cellHeight = 162;
  return cellData;
}

- (Class)cellClazz {
  return NumberPickerCell.class;
}

- (NSString*)nibName {
  return @"NumberPickerCell";
}

- (void)refreshCell:(NumberPickerCell*)cell {
  [super refreshCell:cell];
  if (self.hidden) {
    return;
  }
  cell.pickerView.dataSource = self;
  cell.pickerView.delegate = self;
  [cell.pickerView selectRow:[self indexOfCurrentValue]
                 inComponent:0
                    animated:NO];
  [cell.pickerView reloadAllComponents];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
  return 1;
}

- (NSInteger)pickerView:(UIPickerView*)pickerView
numberOfRowsInComponent:(NSInteger)component {
  return self.values.count;
}

- (NSString*)pickerView:(UIPickerView*)pickerView
            titleForRow:(NSInteger)row
           forComponent:(NSInteger)component {
  return [NSString stringWithFormat:@"%@",self.values[row]];
}

- (void)pickerView:(UIPickerView*)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {
  self.value = [self.values[row] floatValue];
  if ([self.delegate respondsToSelector:@selector(onFloatChanged:cellData:)]) {
    [self.delegate onFloatChanged:self.value cellData:self];
  }
}

- (void)setValue:(float)value {
  _value = value;
}

- (int)indexOfCurrentValue {
  for (int i = 0; i < self.values.count; i++) {
    float number = [self.values[i] floatValue];
    if (self.value == number) {
      return i;
    }
  }
  return 0;
}

@end
